#include "StdAfx.h"
#include "Sprite.h"


CSprite::CSprite(void)
{
	m_nWidth = 0;
	m_nHeight = 0;

	m_nCurPosX = 0;
	m_nCurPosY = 0;
	m_nDstPosX = 0;
	m_nDstPosY = 0;

	m_nTotalFrame = 0;
	m_nCurFrame = 0;
	m_nIntervalTime = 0;
	m_nElapseTime = 0;

	m_bVisible = TRUE;
	m_dwSpriteData = 0;
	m_nSpriteType = 0;
}

CSprite::~CSprite(void)
{
}

CRect CSprite::GetRect()
{
	return CRect(m_nCurPosX, m_nCurPosY, m_nCurPosX + m_nWidth, m_nCurPosY + m_nHeight);
}

BOOL CSprite::IsMove()
{
	return (m_nCurPosX != m_nDstPosX) || (m_nCurPosY != m_nDstPosY) || (m_nCurFrame != m_nTotalFrame);
}

void CSprite::SetPos(int nPosX, int nPosY)
{
	m_nCurPosX = nPosX;
	m_nCurPosY = nPosY;

	m_nDstPosX = nPosX;
	m_nDstPosY = nPosY;
}

void CSprite::SetRect(CRect rc)
{
    SetPos(rc.left, rc.top);
	SetWidth(rc.Width());
	SetHeight(rc.Height());
}

void CSprite::PlayMove(int nDstPosX, int nDstPosy, int nTotalFrame, int nIntervalTime)
{
	m_nDstPosX = nDstPosX;
	m_nDstPosY = nDstPosy;

	m_nCurFrame = 0;
	m_nTotalFrame = nTotalFrame;
	m_nIntervalTime = nIntervalTime;
	m_nElapseTime = 0;
}

void CSprite::PlayFrame(int nTotalFrame, int nIntervalTime)
{
    m_nCurFrame = 0;
	m_nTotalFrame = nTotalFrame;
	m_nIntervalTime = nIntervalTime;
	m_nElapseTime = 0;
}

void CSprite::OnNextFrame()
{
	int nRestFrame = m_nTotalFrame - m_nCurFrame;
	if(nRestFrame <= 0)
		return;

	m_nCurPosX += (m_nDstPosX - m_nCurPosX) / nRestFrame;
	m_nCurPosY += (m_nDstPosY - m_nCurPosY) / nRestFrame;
	m_nCurFrame++;

	if(m_nCurFrame == m_nTotalFrame)
	{
		m_nCurFrame = 0;
		m_nTotalFrame = 0;
		m_nIntervalTime = 0;
		m_nCurPosX = m_nDstPosX;
		m_nCurPosY = m_nDstPosY;
	}
}

void CSprite::EndPlay()
{
	m_nCurFrame = 0;
	m_nTotalFrame = 0;
	m_nIntervalTime = 0;
	m_nCurPosX = m_nDstPosX;
	m_nCurPosY = m_nDstPosY;
}
